#include "udev_include.h"

static unsigned int udev_check_protocol(device_t *device)
{
    const char *name = NULL;
    const char *subsystem = NULL;
    device_t *parent_device = NULL;
    parent_device = udev_device_get_parent(device);
    if (parent_device == NULL) {
        return UNSUPPORTED_PROTOCOL;
    }
    subsystem = udev_device_get_subsystem(parent_device);
    if (subsystem == NULL) {
        return UNSUPPORTED_PROTOCOL;
    }
    name = udev_device_get_sysname(device);
    if (name == NULL) {
        return UNSUPPORTED_PROTOCOL;
    }
    if ((strncmp(name, "sd", 2) != 0) && (strncmp(name, "nvme", 2) != 0)) {
        return UNSUPPORTED_PROTOCOL;
    }
    if (strncmp(subsystem, "scsi", 4) == 0) {
        return SCSI_PROTOCOL;
    }
    if (strncmp(subsystem, "nvme", 4) == 0) {
        return NVME_PROTOCOL;
    }
    return UNSUPPORTED_PROTOCOL;
}

int udev_get_device_info(device_t *device, device_info_t *device_info)
{
    dev_t devnum;
    unsigned int protocol = -1;
    const char *model = NULL;
    const char *devpath = NULL;

    protocol = udev_check_protocol(device);
    if (protocol == UNSUPPORTED_PROTOCOL) {
        return -1;
    }
    devpath = udev_device_get_devnode(device);
    if (devpath == NULL) {
        return -1;
    }
    devnum = udev_device_get_devnum(device);
    if (devnum == -1) {
        return -1;
    }
    model = udev_device_get_property_value(device, "ID_MODEL");
    if (model == NULL) {
        return -1;
    }
    printf("action = %s\n", udev_device_get_action(device));

    device_info->devnum = devnum;
    device_info->protocol = protocol;
    strcpy(device_info->model, model);
    strcpy(device_info->devpath, devpath);
    return 0;
}

int udev_report_device(device_info_t *device_info)
{
    printf("devie: %s\n", device_info->devpath);
    printf("  protocol: %d\n", device_info->protocol);
    printf("  model: %s\n", device_info->model);
    printf("  dev_t: %lu\n", device_info->devnum);
    return 0;
}
